package com.server.pinggu.service.impl;

import cn.hutool.core.codec.Base64;
import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.server.common.constant.HttpStatus;
import com.server.common.core.page.TableDataInfo;
import com.server.pinggu.domain.*;
import com.server.pinggu.dto.LimitInfo;
import com.server.pinggu.dto.PageValue;
import com.server.pinggu.mapper.*;
import com.server.pinggu.service.ISenUserService;
import com.server.pinggu.vo.SenFormsVo;
import com.vdurmont.emoji.EmojiParser;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author ltao
 * @date 2022-10-23
 */
@Service
@AllArgsConstructor
public class SenUserServiceImpl extends ServiceImpl<SenUserMapper, SenUser> implements ISenUserService {

    private final SenUserdataMapper senUserdataMapper;

    @Resource
    private SenActionlogMapper senActionlogMapper;

    @Resource
    private SenFormsRemarkMapper senFormsRemarkMapper;

    @Resource
    private SenFormsMapper senFormsMapper;

    @Override
    public TableDataInfo queryList(SenUser senUser,Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<JSONObject> jsonObjects = this.baseMapper.selectSenUserList(senUser);
        Integer number = this.baseMapper.selectSenUserListTotal(senUser);
        List<JSONObject> collect = jsonObjects.stream().map(a -> {
            if (StringUtils.isNotEmpty(a.getString("nc"))) {
                a.put("nc", new String(Base64.decode(a.getString("nc"))));
            }
            if (StringUtils.isNotEmpty(a.getString("username"))) {
                a.put("username", EmojiParser.parseToAliases(a.getString("username")));
            }
            return a;
        }).collect(Collectors.toList());
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setMsg("查询成功");
        rspData.setRows(collect);
        rspData.setTotal(number);
        return rspData;
    }

    @Override
    public JSONObject getUserInfoById(Long id) {
        return this.baseMapper.getUserInfoById(id);
    }

    @Override
    public void addUser(SenUser senUser) {
        String name = senUser.getUsername();
        senUser.setLasttime(System.currentTimeMillis()/1000);
        senUser.setDttime(System.currentTimeMillis()/1000);
        senUser.setGroupid("4");
        senUser.setPwd("0");
        senUser.setBid(0L);
        senUser.setLognum(1L);
        senUser.setType(0);
        senUser.setExpirationTime(0L);
        senUser.setCurrentStatus(0);
        senUser.setIsdisable(0) ;
        senUser.setRenewtype(0) ;
        senUser.setIstrue(1);
        this.baseMapper.insert(senUser);
        if (senUser.getId()!=null&&senUser.getId()!=0L){
            SenUser update = new SenUser();
            update.setId(senUser.getId());
            update.setUsername("hf-"+senUser.getId());
            this.baseMapper.updateById(update);
            SenUserdata senUserdata = new SenUserdata();
            senUserdata.setId(senUser.getId());
            senUserdata.setRealname(name);
            senUserdata.setRealnamebase64(Base64.decodeStr(name));
            senUserdata.setStreetid(0L);
            senUserdata.setJuweiId(0L);
            senUserdata.setAds(" ");
            senUserdata.setSerialDays(0L);
            senUserdata.setUnpay(new BigDecimal(0));
            senUserdata.setPgsq(0);
            senUserdata.setPgyid(0);
            senUserdata.setHeadimgurl(senUser.getHeadimageUrl());
            senUserdataMapper.insert(senUserdata);
        }
    }

    @Override
    public Long getformsInfoByOpenId(String openId) {
        return this.baseMapper.getformsInfoByOpenId(openId);
    }

    @Override
    public PageValue queryList2(SenFormsVo senForms, Integer sqsjFront, Integer sqsjBack, Integer pageNum, Integer pageSize,String statusStr) {
        LimitInfo limitInfo = new LimitInfo(pageNum, pageSize);
        List<String> strings = null;
        if(com.server.common.utils.StringUtils.isNotEmpty(statusStr)){
            strings = Arrays.asList(statusStr.split(","));
        }
        List<SenForms> result = this.baseMapper.selectSenFormsList2(senForms,limitInfo,strings);
        Integer total = senFormsMapper.selectSenFormsList2Total(senForms, null, null,strings);
        if (CollectionUtil.isNotEmpty(result)){
            List<Long> ids = result.stream().map(SenForms::getId).collect(Collectors.toList());
            List<SenActionlog> list= senActionlogMapper.selectByFormId(ids);
            List<SenFormsRemark> remarkList = senFormsRemarkMapper.selectNewestRemark(ids);
            Map<Long, Long> collect = null;
            Map<Long, String> collectRemark = null;
            if(CollectionUtil.isNotEmpty(list)){
                collect = list.stream().collect(Collectors.toMap(SenActionlog::getFormid, SenActionlog::getId));
            }
            if(CollectionUtil.isNotEmpty(remarkList)){
                collectRemark = remarkList.stream().collect(Collectors.toMap(SenFormsRemark::getFid,SenFormsRemark::getRemark));
            }
            for (SenForms forms : result) {
                if (CollectionUtil.isNotEmpty(collect)){
                    Long aLong = collect.get(forms.getId());
                    if (aLong!=null){
                        forms.setLogTotal(aLong.intValue());
                    }else{
                        forms.setLogTotal(0);
                    }
                }
                SenFormsServiceImpl.setImage(forms);
                if (CollectionUtil.isNotEmpty(collectRemark)){
                    String remark = collectRemark.get(forms.getId());
                    if (com.server.common.utils.StringUtils.isNotEmpty(remark)){
                        forms.setRemark(remark);
                    }
                }
            }
        }
        PageValue pageValue = new PageValue();
        pageValue.setList(result);
        pageValue.setTotal(total);
        return pageValue;
    }

    @Override
    public JSONObject determiningUserRights(String openId) {
        return null;
    }

}
